package com.cqc.lite.web;


import com.cqc.lite.domain.Orders;
import com.cqc.lite.service.OrdersService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
import java.util.List;

@Controller
@RequestMapping("/orders/")
public class OrderController {


    private final OrdersService ordersService;

    @Autowired
    public OrderController(OrdersService ordersService){
        this.ordersService=ordersService;
    }


//    @GetMapping("findAll.do")
//    public ModelAndView findAll(){
//        List<Orders> ordersList = ordersService.findAll();
//        ModelAndView mv=new ModelAndView();
//        mv.addObject("ordersList",ordersList);
//        mv.setViewName("orders-page-list");
//        return mv;
//    }

//  http://localhost:8080/lite_web/orders/findAll.do?page=1&size=10
//    @GetMapping("findAll.do")
    @PermitAll
    @RequestMapping(value = "findAll.do",method = RequestMethod.GET)
    public ModelAndView findAll(@RequestParam(value = "page",required = true,defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "size",required = true,defaultValue = "10") Integer pageSize) throws Exception {
        List<Orders> list = ordersService.findAll(pageNum,pageSize);
        PageInfo<Orders> pageInfo=new PageInfo<>(list);
        ModelAndView mv=new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }

    @DenyAll
    @GetMapping("findById.do")
    public ModelAndView findById(String id) throws Exception {
        Orders orders = ordersService.findById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("orders",orders);
        mv.setViewName("orders-show");
        return mv;
    }
}
